package breeze.lepei.der.loginview.view;
import android.app.ActivityOptions;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.transition.Slide;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import breeze.lepei.der.BaseView;
import breeze.lepei.der.R;
import breeze.lepei.der.loginview.presenter.ILoginPresenter;
import breeze.lepei.der.loginview.presenter.LoginPresenter;
import breeze.lepei.der.logo_view.view.LogoView;
import breeze.lepei.der.main_view.view.MainView;
import breeze.lepei.der.utils.MDialog;

public class LoginView extends BaseView implements ILoginView,OnClickListener {

	/**
	*@author BREEZE
	*@description 跳转界面
	*/
	@Override
	public void jumpView() {
		runOnUiThread(new Runnable(){public void run(){
					stopLoading();
					startActivity(new Intent(mContext,MainView.class));
					finish();
		}});
	}

	/**
	*@author BREEZE
	*@description 显示提示框
	*@param 内容
	*/
	@Override
	public void showDialog(final String message) {
		runOnUiThread(new Runnable(){public void run(){
			stopLoading();
			MDialog.show(LoginView.this,"提示",message);
		}});
	}

	/**
	*@author BREEZE
	*@description 关闭加载
	*/
	@Override
	public void stopLoading() {
		if(progressDialog!=null){
			if(progressDialog.isShowing()){
				progressDialog.setCancelable(true);
				progressDialog.dismiss();
			}
		}
	}

	
	/**
	*@author BREEZE
	*@description 显示加载框
	*/
	@Override
	public void showLoading() {
		if(progressDialog!=null){
			if(progressDialog.isShowing()){
				progressDialog.setCancelable(true);
				progressDialog.dismiss();
			}else{
				progressDialog.setCancelable(false);
				progressDialog.show();
			}
		}
	}

	@Override
	public void onClick(View p1) {
		switch(p1.getId()){
			case R.id.login_about_account:
				//TODO：提示关于账户
				MDialog.show(LoginView.this,getString(R.string.about_account_title),getString(R.string.about_account));
				break;
			case R.id.login_login_btn:
				//TODO：登录
				String phone=edi_phone.getText().toString();
				String password=edi_pass.getText().toString();
				if("".equals(phone)){
					edi_phone.setError("请输入手机号");
				}else if("".equals(password)){
					edi_pass.setError("请输入密码");
				}else if(password.length()<6){
					edi_pass.setError("密码不能少于6位");
				}else{
					showLoading();
					presenter.login(phone,password);
				}
				break;
		}
	}

	@Override
	public void init() {
		edi_phone=f(R.id.login_phone_edi);
		edi_pass=f(R.id.login_pass);
		about_account=f(R.id.login_about_account);
		login=f(R.id.login_login_btn);
	}

	@Override
	public void initData() {
		presenter=new LoginPresenter(this);
		//初始化加载
		progressDialog=new ProgressDialog(LoginView.this);
		progressDialog.setMessage("加载中......");
		progressDialog.setCancelable(false);
		about_account.setOnClickListener(this);
		login.setOnClickListener(this);
	}

    public static final String TAG = "LoginView";
	private EditText edi_phone;
	private EditText edi_pass;
	private ImageView about_account;
	private ProgressDialog progressDialog;
	private Button login;
	private ILoginPresenter presenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setEnterTransition(new Slide());
		if(Build.VERSION.SDK_INT>=21){
			getWindow().setStatusBarColor(Color.TRANSPARENT);
			getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		}
		setContentView(getView(R.layout.view_login));
		init();
		initData();
	}
	
}
